<?php

namespace app\zftpay\controller;

use app\BaseController;
use app\service\alipayWapService;
use app\service\propertyService;
use app\service\webService;
use think\facade\Db;
use think\facade\Log;
use think\facade\View;

class Wap extends AlipayAuth
{
    public function trade(){
        session("url",$this->getCurrentUrl().request()->url());
        $user=Db::table("zft_black_user")->where("user_id",session('user_id'))->find();
        if(!empty($user)){
            return "<text style='font-size:50px;margin: 100px'>你无权支付</text>";
        }
        $key=env("APP.ZFT_KEY");
        $id=request()->get("id");
        $sn=request()->get("sn");
        $money=request()->get("money");
        $str="id=".$id."&sn=".$sn."&money=".$money;
        $sign=md5($key.$str.$key);
        if($sign!=request()->get("sign")){
            return "<text style='font-size:50px;margin: 100px'>支付错误,请重新支付</text>";
        }
        if (!is_numeric($money)) {
            return "<text style='font-size:50px;margin: 100px'>支付金额错误</text>";
        }
        if ($money>50) {
            return "<text style='font-size:50px;margin: 100px'>支付金额不能超过50元</text>";
        }

        $gateinfo=Db::table("community_zft_gate")->where("id",$id)->find();
        if(empty($gateinfo)){
            return "<text style='font-size:50px;margin: 100px'>配置信息错误</text>";
        }
        if($gateinfo["pay_status"]=="0"){
            return "<text style='font-size:50px;margin: 100px'>支付商户状态异常</text>";
        }
        View::assign("info",$gateinfo);
        View::assign("money",$money);
        View::assign("sn",$sn);
        return  View::fetch("sn_money");
        $propertyZftInfo=(new propertyService())->propertyZftByComId($gateinfo["community_id"]);
        if(empty($propertyZftInfo)){
            parent::sendOutput("直付通配置为空",500);
            parent::sendOutput(['info' => '直付通配置为空'],500);
            return json(['info' => '直付通配置为空']);
        }

        $info["property_id"]=$propertyZftInfo["property_id"];
        $info["order_money"]=$money;
        $info["remark"]=request()->post("remark");
        //$info["user_id"]=session("user_id");
        $info["community_name"]=$gateinfo["community_name"];
        $info["pay_money"]=$money;
        $info["out_trade_no"]=$sn;
        $info["subject"] = $gateinfo["community_name"]."-".$gateinfo["gate_name"];
        $info["community_id"]=$gateinfo["community_id"];
        $info["gate_name"]=$gateinfo["gate_name"];
        $info["gate_id"]=$gateinfo["id"];
        $api = new alipayWapService();

        $notifyUrl=$this->getCurrentUrl()."/zftpay/callback/wapNotice";

        $settle_detail_info=array(
            'trans_in_type'=>'loginName',//'loginName',
            'trans_in'=>$propertyZftInfo["binding_alipay_logon_id"],//收款账号aliwykd@163.com
            'amount'=>$money,//结算金额
            "settle_entity_type"=>"SecondMerchant",
            "settle_entity_id"=>$propertyZftInfo["merchant_id"]
        );
        $data= array(
            'out_trade_no'=> $info["out_trade_no"],
            'total_amount'=>$money,
            //'buyer_id'=>$info["user_id"],//$info["user_id"],
            'subject' =>$info["subject"],
            'notifyurl'=>$notifyUrl,
            'merchant_id'=>$propertyZftInfo["merchant_id"],
            'notifyUrl'=>$notifyUrl,
            'settle_detail_info'=>$settle_detail_info,
        );
        $info["scenes"]="QUICK_WAP_WAY";
        $res=Db::table("zft_order")->insert($info);
        $result = $api->wapCreate(json_encode($data));
        Log::write($result,"info");
        return $result;

    }
    public function tradeSn(){
        if(request()->isPost()){
            $data=request()->all();
            $id=$data["id"];
            $user=Db::table("zft_black_user")->where("user_id",session('user_id'))->find();
            if(!empty($user)){
                return "<text style='font-size:50px;margin: 100px'>你无权支付</text>";
            }
            $gateinfo=Db::table("community_zft_gate")->where("id",$id)->find();
            if(empty($gateinfo)){
                return "配置信息错误";
            }
            if($gateinfo["pay_status"]=="0"){
                return "支付商户状态异常";
            }
            $out_trade_no=request()->post("sn");
            $money=request()->post("money");
            $propertyZftInfo=(new propertyService())->propertyZftByComId($gateinfo["community_id"]);
            if(empty($propertyZftInfo)){
                parent::sendOutput("直付通配置为空",500);
                parent::sendOutput(['info' => '直付通配置为空'],500);
                return json(['info' => '直付通配置为空']);
            }

            $info["property_id"]=$propertyZftInfo["property_id"];
            $info["order_money"]=$money;
            $info["remark"]=request()->post("remark");
            $info["user_id"]=session("user_id");
            $info["community_name"]=$gateinfo["community_name"];
            $info["pay_money"]=$money;
            $info["out_trade_no"]=$out_trade_no;
            $info["subject"] = $gateinfo["community_name"]."-".$gateinfo["gate_name"];
            $info["community_id"]=$gateinfo["community_id"];
            $info["gate_name"]=$gateinfo["gate_name"];
            $info["gate_id"]=$gateinfo["id"];
            $api = new alipayWapService();

            $notifyUrl=$this->getCurrentUrl()."/zftpay/callback/wapNotice";

            $settle_detail_info=array(
                'trans_in_type'=>'loginName',//'loginName',
                'trans_in'=>$propertyZftInfo["binding_alipay_logon_id"],//收款账号aliwykd@163.com
                'amount'=>$money,//结算金额
                "settle_entity_type"=>"SecondMerchant",
                "settle_entity_id"=>$propertyZftInfo["merchant_id"]
            );
            $data= array(
                'out_trade_no'=> $info["out_trade_no"],
                'total_amount'=>$money,
                //'buyer_id'=>$info["user_id"],//$info["user_id"],
                'subject' =>$info["subject"],
                'notifyurl'=>$notifyUrl,
                'merchant_id'=>$propertyZftInfo["merchant_id"],
                'notifyUrl'=>$notifyUrl,
                'settle_detail_info'=>$settle_detail_info,
            );
            $info["scenes"]="QUICK_WAP_WAY";
            $res=Db::table("zft_order")->insert($info);
            $result = $api->wapCreate(json_encode($data));
            Log::write($result,"info");
            return $result;
        }
    }
    public function pay(){
        if(request()->isPost()){
            $data=request()->all();
            $id=$data["id"];
            $gateinfo=Db::table("community_zft_gate")->where("id",$id)->find();
            if(empty($gateinfo)){
                return "配置信息错误";
            }
            if($gateinfo["pay_status"]=="0"){
                return "支付商户状态异常";
            }
            $out_trade_no="wap".date('YmdHiss').rand(100000,999999);;
            $money=request()->post("WIDtotal_amount");
            $propertyZftInfo=(new propertyService())->propertyZftByComId($gateinfo["community_id"]);
            if(empty($propertyZftInfo)){
                parent::sendOutput("直付通配置为空",500);
                parent::sendOutput(['info' => '直付通配置为空'],500);
                return json(['info' => '直付通配置为空']);
            }

            $info["property_id"]=$propertyZftInfo["property_id"];
            $info["order_money"]=$money;
            $info["remark"]=request()->post("remark");
            $info["user_id"]=session("user_id");
            $info["community_name"]=$gateinfo["community_name"];
            $info["pay_money"]=$money;
            $info["out_trade_no"]=$out_trade_no;
            $info["subject"] = $gateinfo["community_name"]."-".$gateinfo["gate_name"];
            $info["community_id"]=$gateinfo["community_id"];
            $info["gate_name"]=$gateinfo["gate_name"];
            $info["gate_id"]=$gateinfo["id"];
            $api = new alipayWapService();

            $notifyUrl=$this->getCurrentUrl()."/zftpay/callback/wapNotice";

            $settle_detail_info=array(
                'trans_in_type'=>'loginName',//'loginName',
                'trans_in'=>$propertyZftInfo["binding_alipay_logon_id"],//收款账号aliwykd@163.com
                'amount'=>$money,//结算金额
                "settle_entity_type"=>"SecondMerchant",
                "settle_entity_id"=>$propertyZftInfo["merchant_id"]
            );
            $data= array(
                'out_trade_no'=> $info["out_trade_no"],
                'total_amount'=>$money,
                //'buyer_id'=>$info["user_id"],//$info["user_id"],
                'subject' =>$info["subject"],
                'notifyurl'=>$notifyUrl,
                'merchant_id'=>$propertyZftInfo["merchant_id"],
                'notifyUrl'=>$notifyUrl,
                'settle_detail_info'=>$settle_detail_info,
            );
            $info["scenes"]="QUICK_WAP_WAY";
            $res=Db::table("zft_order")->insert($info);
            $result = $api->wapCreate(json_encode($data));
            Log::write($result,"info");
            return $result;

        }else{
            $id=request()->get("id");
            View::assign("id",$id);
            return View::fetch("pay");
        }

    }
    public function pay2(){
        $data=request()->all();
        $id=$data["id"];
        $gateinfo=Db::table("community_zft_gate")->where("id",$id)->find();
        if(empty($gateinfo)){
            return "配置信息错误";
        }
        if($gateinfo["pay_status"]=="0"){
            return "支付商户状态异常";
        }
        $out_trade_no="wap".date('YmdHiss').rand(100000,999999);;
        $money=request()->post("WIDtotal_amount");
        $propertyZftInfo=(new propertyService())->propertyZftByComId($gateinfo["community_id"]);
        if(empty($propertyZftInfo)){
            parent::sendOutput("直付通配置为空",500);
            parent::sendOutput(['info' => '直付通配置为空'],500);
            return json(['info' => '直付通配置为空']);
        }

        $info["property_id"]=$propertyZftInfo["property_id"];
        $info["order_money"]=$money;
        $info["remark"]=request()->post("remark");
        //$info["user_id"]=session("user_id");
        $info["community_name"]=$gateinfo["community_name"];
        $info["pay_money"]=$money;
        $info["out_trade_no"]=$out_trade_no;
        $info["subject"] = $gateinfo["community_name"]."-".$gateinfo["gate_name"];
        $info["community_id"]=$gateinfo["community_id"];
        $info["gate_name"]=$gateinfo["gate_name"];
        $info["gate_id"]=$gateinfo["id"];
        $api = new alipayWapService();

        $notifyUrl=$this->getCurrentUrl()."/zftpay/callback/wapNotice";

        $settle_detail_info=array(
            'trans_in_type'=>'loginName',//'loginName',
            'trans_in'=>$propertyZftInfo["binding_alipay_logon_id"],//收款账号aliwykd@163.com
            'amount'=>$money,//结算金额
            "settle_entity_type"=>"SecondMerchant",
            "settle_entity_id"=>$propertyZftInfo["merchant_id"]
        );
        $data= array(
            'out_trade_no'=> $info["out_trade_no"],
            'total_amount'=>$money,
            //'buyer_id'=>$info["user_id"],//$info["user_id"],
            'subject' =>$info["subject"],
            'notifyurl'=>$notifyUrl,
            'merchant_id'=>$propertyZftInfo["merchant_id"],
            'notifyUrl'=>$notifyUrl,
            'settle_detail_info'=>$settle_detail_info,
        );
        $info["scenes"]=3;
        $res=Db::table("zft_order")->insert($info);
        $result = $api->wapCreate(json_encode($data));
        Log::write($result,"info");
        return $result;

    }
    public function info(){
        if(request()->isPost()){
            $data=request()->all();
            $id=$data["id"];
            $gateinfo=Db::table("community_zft_gate")->where("id",$id)->find();
            if(empty($gateinfo)){
                return "配置信息错误";
            }
            if($gateinfo["pay_status"]=="0"){
                return "支付商户状态异常";
            }
            $out_trade_no="wap".date('YmdHiss').rand(100000,999999);;
            $money=request()->post("money");
            $propertyZftInfo=(new propertyService())->propertyZftByComId($gateinfo["community_id"]);
            if(empty($propertyZftInfo)){
                parent::sendOutput("直付通配置为空",500);
                parent::sendOutput(['info' => '直付通配置为空'],500);
                return json(['info' => '直付通配置为空']);
            }

            $info["property_id"]=$propertyZftInfo["property_id"];
            $info["order_money"]=$money;
            $info["remark"]=request()->post("remark");
            $info["user_id"]=session("user_id");
            $info["community_name"]=$gateinfo["community_name"];
            $info["pay_money"]=$money;
            $info["out_trade_no"]=$out_trade_no;
            $info["subject"] = $gateinfo["community_name"]."-".$gateinfo["gate_name"];
            $info["community_id"]=$gateinfo["community_id"];
            $info["gate_name"]=$gateinfo["gate_name"];
            $info["gate_id"]=$gateinfo["id"];
            $api = new alipayWapService();

            $notifyUrl=$this->getCurrentUrl()."/zftpay/callback/wapNotice";

            $settle_detail_info=array(
                'trans_in_type'=>'loginName',//'loginName',
                'trans_in'=>$propertyZftInfo["binding_alipay_logon_id"],//收款账号aliwykd@163.com
                'amount'=>$money,//结算金额
                "settle_entity_type"=>"SecondMerchant",
                "settle_entity_id"=>$propertyZftInfo["merchant_id"]
            );
            $data= array(
                'out_trade_no'=> $info["out_trade_no"],
                'total_amount'=>$money,
                //'buyer_id'=>$info["user_id"],//$info["user_id"],
                'subject' =>$info["subject"],
                'notifyurl'=>$notifyUrl,
                'merchant_id'=>$propertyZftInfo["merchant_id"],
                'notifyUrl'=>$notifyUrl,
                'settle_detail_info'=>$settle_detail_info,
            );
            $info["scenes"]="QUICK_WAP_WAY";
            $res=Db::table("zft_order")->insert($info);
            $result = $api->wapCreate(json_encode($data));
            Log::write($result,"info");
            return $result;
        }else{
            session("url",$this->getCurrentUrl().request()->url());
            $id=request()->get("id");
            $money=request()->get("money");
            $info=Db::table("community_zft_gate")->where("id",$id)->find();
            if(empty($info)){
                return "配置信息错误";
            }
            if($info["pay_status"]=="0"){
                return "支付商户状态异常";
            }
            //查询是否是黑名单用户
            $user=Db::table("zft_black_user")->where("user_id",session('user_id'))->find();
            if(!empty($user)){
                return "<text style='font-size:50px;margin: 100px'>你无权支付</text>";
            }
            View::assign("money",$money);
            View::assign("info",$info);
            $notifyurl_alipay=(new webService())->getNotifyurlAlipay();
            View::assign("notifyurl_alipay",$notifyurl_alipay);
            return View::fetch("info");
        }

    }

}